<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
        }

        .container {
            max-width: 400px;
            margin: 50px auto;
            padding: 30px;
            background-color: white;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        label {
            font-size: 14px;
            color: #555;
            display: block;
            margin-bottom: 8px;
        }

        input[type="text"], input[type="password"] {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        .register-link {
            display: block;
            text-align: center;
            margin-top: 10px;
        }

        .register-link a {
            text-decoration: none;
            color: #007BFF;
            font-size: 14px;
        }

        .register-link a:hover {
            color: #0056b3;
        }
    </style>
    <script src="js/jquery-3.7.1.js"></script>
</head>
<body>

<div class="container">
    <h1>用户登录</h1>
    <form id="loginForm" action="login" method="post">
        <label for="username">用户名</label>
        <input type="text" id="username" name="username" placeholder="请输入用户名" required><br>

        <label for="password">密码</label>
        <input type="password" id="password" name="password" placeholder="请输入密码" required><br>

        <button type="submit" id="loginbtn">登录</button><br><br>
    </form>

    <div class="register-link">
        <a href="reg.html">没有账号？点击注册</a>
    </div>
</div>

<script>
    $(document).ready(function() {
        // 绑定表单提交事件
        $('#loginForm').submit(function(event) {
            event.preventDefault(); // 阻止表单的默认提交行为

            const username = $('#username').val();
            const password = $('#password').val();
            console.log("username:="+username,"password:="+password+"a")

            // 验证用户名和密码是否为空
            if (!username || !password) {
                alert("用户名和密码不能为空！");
                return;
            }

            // 发起AJAX请求进行登录
            $.ajax("login",{
                type: 'POST',
                data: {
                    username: username,
                    password: password
                },
                dataType: 'json',
                success: function(response) {
                        if (response.success) {
                            alert(response.message);
                            window.location.href = 'product.html';
                        } else {
                            alert(response.message);
                        }
                },
                error: function(xhr, status, error) {
                    alert('请求失败：' + error);
                }
            });

        });
    });
</script>

</body>
</html>
